import {getLyric} from '@/api/song'
import {Base64} from 'js-base64'
import {ERR_OK} from 'api/config'
// 歌曲类
export default class Song {
  constructor ({id, mid, singer, name, album, duration, image, url}) {
    this.id = id
    this.mid = mid
    this.singer = singer
    this.name = name
    this.album = album
    this.duration = duration
    this.image = image
    this.url = url
  }

  // 拿到歌词数据
  getSongLyric () {
    // 当已经请求过歌词时，不必再请求
    if (this.lyric) {
      return Promise(this.lyric)
    }
    return new Promise((resolve, reject) => {
      getLyric(this.mid).then((res) => {
        // 解码base64数据
        if (res.retcode === ERR_OK) {
          this.lyric = Base64.decode(res.lyric)
          resolve(this.lyric)
        } else {
          reject(new Error('no lyric'))
        }
      })
    })
  }
}

// 工厂模式创建歌曲
export function createSong (musicData, vkey) { // 格式化歌曲信息
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    // https://y.gtimg.cn/music/photo_new/T002R300x300M000 001yYgIv3a4YEn .jpg?max_age=2592000
    // 薛之谦<丑八怪>的url
    url: `http://isure.stream.qqmusic.qq.com/C400003mBrF72dILfK.m4a?guid=461236210&vkey=6468020A7E064944926E6AF121A062C53F94546463C4AA50A7147B75E30ACA1F6E4974D3FC3CC8ECA236AF36754FAEE8235E4370C314C7F4&uin=0&fromtag=66`
    // 此方法已失效
    // url: `http://isure.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?guid=461236210&vkey=${vkey}&uin=0&fromtag=66`
  })
}

// 由于歌单详情列表跟歌手详细歌曲列表数据结构不一样，
// 所以这里暂时重新创建一个歌曲类
export function createSongForDisc (musicData) { // 格式化歌曲信息
  return new Song({
    id: musicData.id,
    mid: musicData.mid,
    singer: filterSinger(musicData.singer),
    name: musicData.name,
    album: musicData.album.name,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.album.pmid}.jpg?max_age=2592000`,
    // 薛之谦<演员>的url
    url: `http://isure.stream.qqmusic.qq.com/C400003mBrF72dILfK.m4a?guid=461236210&vkey=28EDD8261AC8EA05D0C0E17C8420D5DEA0FC9B062CC9960435B102D38EBC7D1103C54FCF7B5F60E311D367953BB3BABDC6224266549194C5&uin=0&fromtag=66`
  })
}

// 为搜索列表创建歌曲类
export function createSongForSearch (musicData) {
  return new Song({
    id: musicData.albumid,
    mid: musicData.albummid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    // 薛之谦<演员>的url
    url: `http://isure.stream.qqmusic.qq.com/C400003mBrF72dILfK.m4a?guid=461236210&vkey=0403D0B689C74D3940CF912A4B36692BC03D2A32F6F68580B33E745EF5A48AD6983923165F05DB32B1660635CBB01039CEB0F4B02B7DDDD4&uin=0&fromtag=66`
  })
}

// 因为QQ官网返回的数据是可能同时有多个歌手的
// 所以这里要对歌手名字处理一下
function filterSinger (singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  // 返回的多个歌手之间以“/” 连接
  return ret.join('/')
}
